package com.example.cmpe277stockdata;

//import com.example.cmpe277stockdata.LoginDataBaseAdapter;
//import com.example.cmpe277stockdata.SignUPActivity;

import java.net.MalformedURLException;

import com.example.cmpe277stockdata.beans.UserBean;
import com.example.cmpe277stockdata.constants.DataConstants;
import com.example.cmpe277stockdata.dataaccess.AzureLogin;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	Button btnSignIn,btnSignUp;
	//LoginDataBaseAdapter loginDataBaseAdapter;
	AzureLogin loginazure;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		// create a instance of SQLite Database
	    /* loginDataBaseAdapter=new LoginDataBaseAdapter(this);
	     loginDataBaseAdapter=loginDataBaseAdapter.open();*/
	     
	     // Get The Reference Of Buttons
	     btnSignIn=(Button)findViewById(R.id.buttonSignIn);
	     btnSignUp=(Button)findViewById(R.id.buttonSignUp);
	     
	  // Set OnClick Listener on SignUp button 
		    btnSignUp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				/// Create Intent for SignUpActivity  and Start The Activity
				Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
				startActivity(intentSignUP);
				}
			});
	//}

	// Methos to handleClick Event of SignIn Button
	// get the Refferences of views
	final  EditText editTextUserName=(EditText)findViewById(R.id.editTextUserNameToLogin);
	final  EditText editTextPassword=(EditText)findViewById(R.id.editTextPasswordToLogin);
	    
	 // Set On ClickListener
	 btnSignIn.setOnClickListener(new View.OnClickListener()
	 {
		 public void onClick(View v) 
		 {
			 // get The User name and Password
			 String userName=editTextUserName.getText().toString();
			 String password=editTextPassword.getText().toString();
	 					
			
		 	 // fetch the Password form database for respective user name
		 	// boolean validLogin=loginDataBaseAdapter.validateUser(userName,password);
			 try {
					loginazure = new AzureLogin(getApplicationContext());
					 boolean validLogin = loginazure.validateUser(userName, password);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		 	
		 	/* if(validLogin)
		 	 {
		 		 //UserBean user = loginDataBaseAdapter.getUserDetails(userName);
		 		UserBean user = loginazure.getUserDetails(userName);
		 		 
		 		 Intent intent = new Intent(getApplicationContext(),InitialTabContent.class);
		 		 intent.putExtra(DataConstants.USER_DATA, user);
		 		 startActivity(intent);
		 	 }
		 	 else
		 	 {
		 		 Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
		 	 }*/
		 					
//		 	 // check if the Stored password matches with  Password entered by user
//		 	 if(password.equals(storedPassword))
//		 	 {
//		 		 Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
//		 	 }
//		 	 else
//		 	 {
//		 		 Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
//		 	 }
		  }
	  });
	 	
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    // Close The Database
		//loginDataBaseAdapter.close();
	}

}
